package cn.edu.hzw.criminalintent.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import cn.edu.hzw.criminalintent.fragmnet.CrimeFragment
import cn.edu.hzw.criminalintent.R
import cn.edu.hzw.criminalintent.databinding.ActivityMainBinding
import cn.edu.hzw.criminalintent.fragmnet.CrimeListFragment
import java.util.*

private const val TAG = "MainActivity"
class MainActivity : AppCompatActivity(),CrimeListFragment.Callbacks {
    private lateinit var mBinding:ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        val currentFragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
        if (currentFragment==null){
            /**
             * 创建一个新的fragment事务，执行一个fragment添加操作，然后提交该事务
             */
            val fragment = CrimeListFragment.newInstance()
            supportFragmentManager.beginTransaction()
                .add(R.id.fragment_container,fragment)
                .commit()
        }
    }

    override fun onCrimeSelected(crimeId: UUID) {
//        Log.d(TAG, "MainActivity.onCrimeSelected:$crimeId")
        val fragment = CrimeFragment.newInstance(crimeId)
        supportFragmentManager.beginTransaction()
            .replace(R.id.fragment_container,fragment)
            .addToBackStack(null)
            .commit()
    }
}